
/**
 * This example explores String comparisons
 * @author Amrita Mukherjee
 * @version 15/10/2012
 */
public class stringComparison
{
    public static void main(String[] args)
    {
        // these are three different String variables
        //they are defined at "compile time"
        //Since s1 and s2 are strings and they ahve the same contents,
        //how ever s1 and s2 refer to teh same object!
        String S1 = "rocket";
        String S2 = "rocket";
        String S3 = "ROCKET";
        
        //Let's see:
        //compare references
        System.out.println("\nS1: " + S1);
        System.out.println("S2: " + S2);
        if(S1==S2)
        {
            System.out.println("S1 amd S2 refer the same String");    
        }
        else
        {
            System.out.println("S1 and S2 DO NOT refer the same string.");
        }
        
        //Now, use a capability to compare the contents
        if(S1.equals(S2))
        { //compare the contects
            System.out.println("S1 and S2 have same content");
        }
        else
        {
            System.out.println("S1 and S2 do not have the same contents");
        }
        
        System.out.println("\nS1: " + S1);
        System.out.println("S3: " + S3);
        if(S1==S3)
        {
            System.out.println("S1 and S3 refer to the same string");
        }
        else
        {
            System.out.println("S1 and S3 do NOt refer the same stirng");
        }
        
        //Create a new object(at runtime) whole contents is identical to S1
        String S4 = S3.toLowerCase();
        
        System.out.println("\nS1: " + S1 );
        System.out.println("S4: " + S4);
        if(S1 == S4 )
        {
            System.out.println("S1 and S4 refer to the same string");
        }
        else
        {
            System.out.println("S1 and S4 do not refer to the same string");
        }
        if(S1.equals(S4))
        { //compare contents
            System.out.println("S1 ans S4 have the same content");
        }
        else
        {
            System.out.println("S1 and S4 do not have same contents");
        }
    }
}
/*
 * my test session:
 * S1: rocket
S2: rocket
S1 amd S2 refer the same String
S1 and S2 have same content

S1: rocket
S3: ROCKET
S1 and S3 do NOt refer the same stirng

S1: rocket
S4: rocket
S1 and S4 do not refer to the same string
S1 ans S4 have the same content

 */